const axios = require('axios');
const kuakuaRobotService = require('../services/kuakuaRobotService');
const crypto = require('@wecom/crypto');
const log = require('../../logs/log');
const xml2js = require('xml2js');

class KuakuaController {

    async test(req,res){
        const logger = log.logger;
        logger.info("time:", new Date());
        logger.error("time:", new Date());
        const kuakua = await new kuakuaRobotService();
        res.send(await kuakua.get_kuakua_word())
    }

    async url_verification(req,res){
        const sEncodingAESKey = "RQGhS9c3OtfdtYpd6Sk9hyQqXeap9y7HexmKMMFWV2y";
        const eStr = crypto.decrypt(sEncodingAESKey,req.query.echostr);
        res.send(eStr.message);
    }

    async msg_receive(req,res){
        const logger = log.logger;
        const kuakua = await new kuakuaRobotService();

        // 如果监听到消息中含有"夸夸我"，则随机回复一句夸人的话
        const sEncodingAESKey = "RQGhS9c3OtfdtYpd6Sk9hyQqXeap9y7HexmKMMFWV2y";
        logger.info("接收消息成功");
        let buffers = [];

        req.on('data', function (trunk) {
            buffers.push(trunk);
        });

        req.on('end', function () {
            logger.info(Buffer.concat(buffers).toString('utf-8'));
            // 提取用户信息
            let xml = Buffer.concat(buffers).toString('utf-8');
            xml2js.parseString(xml,{trim: true},async function (err, result) {
                // 抽取数据并解密
                const decrypted = crypto.decrypt(sEncodingAESKey,result.xml.Encrypt[0]);
                xml2js.parseString(decrypted.message,{trim: true},async function (err,result) {
                    // 格式化解密后的信息，并根据信息进行操作
                    console.log(result.xml);
                    let msgType = result.xml.MsgType[0];
                    let content = result.xml.Content[0];
                    let fromuser = result.xml.FromUserName[0];
                    switch (msgType) {
                        case 'text':
                            if(content.indexOf("夸") >= 0){
                                await kuakua.send_text_to_user(await kuakua.get_kuakua_word(),fromuser);
                            }else if(content.indexOf("骂") >= 0){
                                await kuakua.send_text_to_user(await kuakua.get_maren_word(),fromuser);
                            }else {
                                await kuakua.send_text_to_user(await kuakua.get_kuakua_word(),fromuser);
                            }
                            break;
                        //其他逻辑根据业务需求进行处理
                        case 'image':
                            break;
                        case 'video':
                            break;
                        case 'voice':
                            break;
                        case 'location':
                            break;
                        case 'link':
                            break;
                        case 'event':
                            break;
                    }
                })

            })
        });

        res.send("success");
    }

}
module.exports = new KuakuaController();
